package com.gao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Projectname springboot-04-data
 * @Filename JDBCcontroller
 * @Author an
 * @Data 2022/8/11 9:07
 * @Description TODO
 */
@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据库的所有信息
    //没有实体类。数据库中的东西怎么获取？ map
    @GetMapping("/userlist")
    public List<Map<String,Object>> userList(){
        String sql ="select * from t_user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

        return maps;
    }

    @GetMapping("/adduser")
    public String addUser(){
        String sql = "insert into t_user(username,password,age,gender,email) " +
                "values ('小明',1234,20,'男','123@qq.com')";
        jdbcTemplate.update(sql);

        return "update-ok";
    }

    @GetMapping("/updateuser/{id}")
    public String updateUser(@PathVariable("id") Integer id){
        String sql="update t_user set username=?,password=? where id="+id;
        //封装
        Object[] objects = new Object[2];
        objects[0] = "aaaa";
        objects[1] = 11111;

        jdbcTemplate.update(sql,objects);

        return "updateuser";
    }

    @GetMapping("/deleteuser/{id}")
    public String deleteUser(@PathVariable("id") Integer id){
        String sql = "delete from t_user where id =?";
        jdbcTemplate.update(sql,id);
        return "deleteuser";
    }

}
